import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';
import 'package:search_people/page/dynamic/subs/dynamic_leaders/cell.dart';
import 'package:search_people/page/home/subs/person_detail/models/person_dynamic_model.dart';
import 'package:search_people/utils/component/list/section.dart';

import 'index.dart';

class DynamicController extends GetxController
    with GetSingleTickerProviderStateMixin {
  DynamicController();

  final state = DynamicState();

  void onRefresh() {
    Future.delayed(Duration(seconds: 2)).then((value) {
      if (state.rc.isRefresh) {
        state.rc.refreshCompleted();
      }
    });
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
    state.titles = ['关注', '中央领导', '省级领导', '市级领导', '县区级领导'];
    state.tc = TabController(length: state.titles.length, vsync: this);
    for (var e in state.titles) {
      state.rcs[e] = RefreshController();
      state.dynamicData[e] = [];
      state.swiperData[e] = [
        'https://p1.img.cctvpic.com/photoAlbum/photo/2023/02/10/PHOTuyRhA6OwXvtzrLqGYQQv230210_1000x2000.jpg',
        'https://p4.img.cctvpic.com/photoAlbum/photo/2023/02/10/PHOTVgfe8zgBZBsR41QMv5Px230210_1000x2000.jpg',
        'https://pics7.baidu.com/feed/314e251f95cad1c8983e29b89840d402c83d5136.jpeg@f_auto?token=b759596b1b8eaa2cda8b58c42a91ad60',
      ];
      setupDynamicData(e, ['党委领导动态', '政府领导动态']);
    }
  }

  void setupDynamicData(String t, List data) {
    state.dynamicData[t] = data
        .map((e) => DynamicCardModel(
              title: e,
              list: List.generate(10, (index) => PersonDynamicModel()).toList(),
            ))
        .toList();
    update();
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
    state.tc.dispose();
    state.rcs.forEach((key, value) {
      value.dispose();
    });
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }
}
